/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.doc.server.impl;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.sql.query.DBRule;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.core.sql.result.DataResult;
import com.wallellen.core.time.TimeTool;
import com.wallellen.knowledge.doc.dao.WeburlDaoInter;
import com.wallellen.knowledge.doc.domain.WebUrl;
import com.wallellen.knowledge.doc.server.FileManagerInter;
import com.wallellen.knowledge.doc.server.WeburlServiceInter;
import com.wallellen.knowledge.doc.server.commons.DocumentConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/* *
 *功能：推荐服务服务层实现类
 *详细：
 *
 *版本：v0.1
 *@author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 *日期：20150707114057
 *说明：
 */
@Service
public class WeburlServiceImpl implements WeburlServiceInter {
    @Resource
    private WeburlDaoInter weburlDaoImpl;
    @Resource
    private FileManagerInter farmFileServer;

    @Override
    @Transactional
    public WebUrl insertWeburlEntity(WebUrl entity, LoginUser user) {
        entity.setCuser(user.getId());
        entity.setCtime(TimeTool.getTimeDate14());
        entity.setCusername(user.getName());
        entity.setEuser(user.getId());
        entity.setEusername(user.getName());
        entity.setEtime(TimeTool.getTimeDate14());
        entity.setPstate("1");
        return weburlDaoImpl.insertEntity(entity);
    }

    @Override
    @Transactional
    public WebUrl editWeburlEntity(WebUrl entity, LoginUser user) {
        WebUrl entity2 = weburlDaoImpl.getEntity(entity.getId());
        entity2.setWebname(entity.getWebname());
        entity2.setUrl(entity.getUrl());
        entity2.setEusername(user.getName());
        entity2.setEuser(user.getId());
        entity2.setEtime(TimeTool.getTimeDate12());
        entity2.setFileid(entity.getFileid());
        weburlDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Override
    @Transactional
    public void deleteWeburlEntity(String id, LoginUser user) {

        weburlDaoImpl.deleteEntity(weburlDaoImpl.getEntity(id));
    }

    @Override
    @Transactional
    public WebUrl getWeburlEntity(String id) {

        if (id == null) {
            return null;
        }
        return weburlDaoImpl.getEntity(id);
    }

    @Override
    @Transactional
    public DataQuery createWeburlSimpleQuery(DataQuery query) {

        DataQuery dbQuery = DataQuery
                .init(query, "FARM_WEBURL",
                        "ID,WEBNAME,URL,CTIME,ETIME,CUSERNAME,CUSER,EUSERNAME,EUSER,PCONTENT,PSTATE,SORT,FILEID");
        return dbQuery;
    }

    // ----------------------------------------------------------------------------------
    public WeburlDaoInter getWeburlDaoImpl() {
        return weburlDaoImpl;
    }

    public void setWeburlDaoImpl(WeburlDaoInter dao) {
        this.weburlDaoImpl = dao;
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getList() {
        try {
            DataQuery query = createWeburlSimpleQuery(null);
            query.setNoCount();
            query.setPagesize(100);
            DataResult result = query.search();
            result.runHandle(row -> {
                if (row.get("FILEID") != null && !row.get("FILEID").toString().isEmpty()) {
                    String imgUrl = DocumentConfig.getString("config.doc.download.url") + row.get("FILEID");
                    row.put("IMGURL", imgUrl);
                } else {
                    row.put("IMGURL", "actionImg/Publoadfile.do?id=402888ac500e2c4201500e7a041e0042");

                }
            });
            List<Map<String, Object>> list = result.getResultList();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public void delImg(String imgid) {
        //清空图片字段
        List<WebUrl> list = weburlDaoImpl.selectEntitys(new DBRule("FILEID", imgid, "=").getDBRules());
        for (WebUrl weburl : list) {
            weburl.setFileid(null);
        }

        //删除附件表和附件
        farmFileServer.delFile(imgid, null);
    }
}
